﻿using DreamWork.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace DreamWork.WEBAPIExtensions
{
    public static partial class ViewModelExtensions
    {
        /// <summary>
        /// 列表分页
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="listToPage">分页列表源</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="PageSize">分页大小</param>
        /// <returns></returns>
        public static IEnumerable<TEntity> PageItems<TEntity>(this IEnumerable<TEntity> listToPage, QueryPageInfo pageInfo)
        {
            return listToPage?.Skip(pageInfo.PageSize * ((pageInfo.PageIndex - 1) >= 0 ? (pageInfo.PageIndex - 1) : 0))
            .Take(pageInfo.PageSize);
        }

        public static IEnumerable<TResult> PageItemsWithProcess<TEntity, TResult>(this IEnumerable<TEntity> listToPage
            ,QueryPageInfo pageInfo, Func<TEntity, TResult> processResult)
        {
            return listToPage.PageItems(pageInfo).Select(processResult);
        }
    }
}
